package com.ioudi.digu.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ioudi.digu.dao.support.IQuerySupport;
import com.ioudi.digu.service.support.ListResult;
import com.ioudi.digu.vo.Messager;

public class MessagerDAO extends HibernateDaoSupport
{
	 private IQuerySupport querySupport;
	 

	public boolean saveOrUpdateMessager(Messager messager)
     {
    	boolean result = false;
    	if(messager == null)
    	{
    		return result;
    	}
        Session s = this.getSession();
        try
        {
        	s.beginTransaction();
        	s.saveOrUpdate(messager);
        	s.getTransaction().commit();
        	result = true;
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return result;
    }
	 
	public boolean deleteMessagers(String[] messagerIds)
	{
		boolean result = false;
        Session s = this.getSession();
        s.beginTransaction();
        try
        {
        	int i = messagerIds.length - 1;
        	String sql = "delete Messager m where m.id=:id";
        	while(i >= 0)
        	{
        		s.createQuery(sql).setString("id", messagerIds[i]).executeUpdate();
        	}
        	s.getTransaction().commit();
        	result = true;
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return result;
	}
	
	public Messager getMessager(String messagerID)
    {
		Messager result = null;
	   	if(messagerID == null)
	   	{
	   		return result;
	   	}
        Session s = this.getSession();
        try
        {
	       	s.beginTransaction();
	       	Object temp = s.createQuery("from Messager m where m.id=:messagerID").
	       	    setString("messagerID", messagerID).uniqueResult();
	       	s.getTransaction().commit();
	       if(temp != null)
	       {
	    	   result = (Messager)temp;
	       }
        }
        catch(Exception e)
        {
    	   s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
       return result;
   }
	
	public ListResult findMessage(int index, int maxCount, Map<String, Object> condition)
	{
		String sql = "from Messager m order by m.usedNum desc where ";
		List<String> keys = new ArrayList<String>();
		sql += querySupport.createStatement("m", condition, keys);
        Session s = this.getSession();
        s.beginTransaction();
        ListResult result = new ListResult();
        try
        {
        	Query query = s.createQuery("select count(m) " + sql);
        	Object temp = querySupport.createQuery(query, keys, condition).uniqueResult();
        	int total = 0;
        	if(temp != null)
        	{
        		total = ((Long)temp).intValue();
        	}
        	result.setTotal(total);
        	query = s.createQuery(sql);
        	query = querySupport.createQuery(query, keys, condition);
        	query.setFirstResult(index);
        	if(maxCount > 0)
        	{
        		query.setMaxResults(maxCount);
        	}
        	result.setContents(query.list());
        	s.getTransaction().commit();
        }
        catch(Exception e)
        {
        	result = null;
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
		return result;
	}
	
	public IQuerySupport getQuerySupport()
	{
		return querySupport;
	}

	public void setQuerySupport(IQuerySupport querySupport)
	{
		this.querySupport = querySupport;
	}
}
